package com.zwface.demo.base;

import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.zwface.demo.App;
import com.zwface.demo.R;
import com.zwface.demo.utils.SoftKeyBoardUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.databinding.ViewDataBinding;

/**
 * created by 张中文
 * on 2020/2/2
 * 描述：
 */
public class BaseActivity<T extends ViewDataBinding> extends FrameActivity<T> {

    @Override
    public int onLayoutId() {
        return 0;
    }

    @Override
    public void onInitViewDataBinding(T binding) {

    }

    @Override
    public void onActivityConfigure() {

    }

    @Override
    public Boolean onSetEventBus() {
        return false;
    }

    @Override
    public void onReceiveBundleFromPre(Bundle bundle) {

    }

    @Override
    public void onInitData(Bundle savedInstanceState) {
        SoftKeyBoardUtils.hideSoftInput(this);       //软键盘关闭

        //禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        //沉浸式状态栏
        QMUIStatusBarHelper.translucent(this, ColorUtils.getColor(R.color.transparent));
    }

    @Override
    public void onInitListener() {

    }

    @Override
    public void onInitComponent() {

    }

    @Override
    protected void onDestroy() {
        SoftKeyBoardUtils.hideSoftInput(this);        //软键盘关闭
        super.onDestroy();
    }

    /**
     * 防止快速请求两次
     */
    Map<String,Long> requestTimeStampHashMap = new HashMap<String, Long>(); //上一次请求时间
    int limitRequestTime = 600;   //400毫秒
    public Boolean isDoubleClick(String requestMethodName)
    {      //防止用户连续点击按钮调用
        if (!requestTimeStampHashMap.containsKey(requestMethodName)) {
            requestTimeStampHashMap.put(requestMethodName,0L);
        }

        Boolean res =false;
        if ((System.currentTimeMillis() - requestTimeStampHashMap.get(requestMethodName)) > limitRequestTime)
        {
            requestTimeStampHashMap.put(requestMethodName,System.currentTimeMillis());
            res = false;
        } else {
            res = true;
        }
        return res;
    }

    public void showToast(String msg)
    {
        ToastUtils.showShort(msg);
    }

    public void showLongToast(String msg)
    {
        ToastUtils.showLong(msg);
    }

    public void cancelToast()
    {
        ToastUtils.cancel();
    }

    public void showLoading(String text) {
        showLoading(text,false);
    }

    public void showLoading(String text, Boolean cancleable) {
        App.getInstance().progressDialog = new ProgressDialog(ActivityUtils.getTopActivity());
        App.getInstance().progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        App.getInstance().progressDialog.setMessage(StringUtils.isEmpty(text)?"":text);    //设置内容
        App.getInstance().progressDialog.setCancelable(cancleable);//点击屏幕和按返回键都不能取消加载框
        App.getInstance().progressDialog.show();

        //设置超时自动消失
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //取消加载框
                if(dismissLoading()){
                    //超时处理
                    showToast("请求超时");
                }
            }
        }, 30000);//超时时间30秒
    }

    public Boolean dismissLoading() {
        if (App.getInstance().progressDialog != null){
            if (App.getInstance().progressDialog.isShowing()) {
                App.getInstance().progressDialog.dismiss();
                App.getInstance().progressDialog = null;
                return true;//取消成功
            }
        }
        return false;//已经取消过了，不需要取消
    }

    @Override
    protected void onStop() {
        super.onStop();
        dismissLoading();
    }
}
